<template>
  <div class="app">
    <div class="box">
      <div class="circle-box">
        <div class="circle"></div>
        <div class="circle"></div>
      </div>
      <div class="bg-filter">
        <ul class="bubble-bgwall">
          <li></li>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
        </ul>
        <div class="login-wrap">
          <el-row>
            <el-col :xs="2" :sm="4" :md="6" :lg="8" :xl="9">
              <div class="grid-content"></div>
            </el-col>
            <el-col :xs="20" :sm="16" :md="12" :lg="8" :xl="6">
              <el-form
                ref="registrationForm"
                :rules="rules"
                :model="registrationForm"
                label-width="130px"
                label-position="right"
                class="form login-container"
              >
                <el-form-item class="login-title">
                  <div style="margin-left: 10vh">
                    <h1 style="font-size: 30px">欢迎报名</h1>
                  </div>
                </el-form-item>
                <el-form-item label="姓名" prop="name">
                  <el-input
                    v-model="registrationForm.name"
                    :disabled="isDisabled"
                  ></el-input>
                </el-form-item>
                <el-form-item label="所属院系" required>
                  <el-col :span="11">
                    <el-form-item prop="collegeId">
                      <el-select
                        filterable
                        v-model="registrationForm.collegeId"
                        placeholder="请选择学院"
                        :disabled="isDisabled"
                        @change="colleageChanged(registrationForm.collegeId)"
                      >
                        <el-option
                          v-for="(item, index) in collegeAndMajor"
                          :key="index"
                          :label="item.collegeName"
                          :value="item.collegeId"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col class="line" :span="2" style="text-align: center"
                    >-</el-col
                  >
                  <el-col :span="11">
                    <el-form-item prop="majorId">
                      <el-select
                        v-model="registrationForm.majorId"
                        placeholder="请选择专业"
                        :disabled="isDisabled"
                      >
                        <el-option
                          filterable
                          v-for="(item, index) in majorList"
                          :key="index"
                          :label="item.name"
                          :value="item.id"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-form-item>
                <el-form-item label="电话" prop="phone">
                  <el-input
                    v-model="registrationForm.phone"
                    :disabled="isDisabled"
                  ></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="gender">
                  <el-radio-group
                    v-model="registrationForm.gender"
                    :disabled="isDisabled"
                  >
                    <el-radio :label="0">男</el-radio>
                    <el-radio :label="1">女</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="意向部门" prop="deptId">
                  <el-radio-group
                    v-model="registrationForm.deptId"
                    :disabled="isDisabled"
                  >
                    <el-radio :label="1816762909794357249">项目部</el-radio>
                    <el-radio :label="1816762956233691138">算法部</el-radio>
                    <el-radio :label="1816762935580938241">策划部</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="个人描述" prop="description">
                  <el-input
                    type="textarea"
                    v-model="registrationForm.description"
                    :disabled="isDisabled"
                    :rows="3"
                    :cols="50"
                    resize="none"
                  ></el-input>
                </el-form-item>
                <!--     按钮-->
                <el-form-item class="button-center">
                  <el-button
                    type="primary"
                    @click="mod"
                    v-show="isModBut"
                    :disabled="!isModDisabled"
                    >修改</el-button
                  >
                  <el-button
                    type="primary"
                    @click="cancelMod"
                    v-show="isModshow"
                    >取消修改</el-button
                  >
                  <el-button
                    type="primary"
                    @click="save(registrationForm)"
                    :disabled="isDisabled || isSignUpBtu"
                    >提交</el-button
                  >
                  <el-button
                    @click="resetParam"
                    :disabled="isDisabled || isSignUpBtu"
                    >重置</el-button
                  >
                  <el-button
                    @click="delJoinForm"
                    type="danger"
                    v-show="isCancelDisabled"
                    >取消报名</el-button
                  >
                </el-form-item>
              </el-form></el-col
            >
            <el-col :xs="2" :sm="4" :md="6" :lg="8" :xl="9">
              <div class="grid-content"></div>
            </el-col>
          </el-row>
        </div>
      </div>
    </div>

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <span>这是一段信息</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { collegeQueryAll } from "../../api/user";
import {
  addJoinForm,
  getJoinFormByToken,
  modJoinFormByToken,
  delJoinFormByToken,
} from "../../api/signUp";
export default {
  data() {
    return {
      collegeAndMajor: "", //学院以及专业信息
      majorList: [], //专业列表
      deptsList: [], //部门列表
      isDisabled: false, //控制表单内容是否禁用
      isCancelDisabled: false, //控制表单的取消报名按钮是否禁用
      isModDisabled: false, //控制表单的修改报名按钮是否禁用
      isModshow: false,
      isModBut: false, //控制表单修改报名按钮是否显示
      isSignUp: false, //检查是否报名过
      isSignUpBtu: false, //控制提交按钮
      // 表单参数
      registrationForm: {
        name: "",
        majorId: "",
        collegeId: "",
        phone: "",
        gender: 0,
        description: "",
        deptId: 0,
      },
      rules: {
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 2 到 5 个字符", trigger: "blur" },
        ],
        collegeId: [{ required: true, message: "请选择学院", trigger: "blur" }],
        majorId: [{ required: true, message: "请选择专业", trigger: "blur" }],
        phone: [
          { required: true, message: "电话不能为空", trigger: "blur" },
          {
            pattern: /^[0-9]{10,15}$/,
            message: "电话号码必须是10到15位数字",
            trigger: "blur",
          },
        ],
        gender: [{ required: true, message: "请选择性别", trigger: "change" }],
        deptId: [
          { required: true, message: "请选择意向部门", trigger: "change" },
        ],
        description: [
          { required: true, message: "请填写个人描述", trigger: "blur" },
        ],
        deptId: [{ required: true, message: "请选择部门", trigger: "change" }],
      },
    };
  },
  methods: {
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    resetForm() {
      this.$refs.form.resetFields();
      this.$refs.form.clearValidate(); // 清除验证状态
    },
    resetParam() {
      this.registrationForm.name = "";
      this.registrationForm.collegeId = "";
      this.registrationForm.majorId = "";
      this.registrationForm.phone = "";
      this.registrationForm.gender = 0;
      this.registrationForm.description = "";
      this.$refs.registrationForm.clearValidate(); // 清除验证状态
    },
    // showErrorMessage(field, value) {
    //   // 根据字段的值动态显示或隐藏错误信息
    //   this.$set(this.registrationForm, `error.${field}`, !value);
    // },
    resetErrors() {
      // 重置所有错误信息
      for (let field in this.registrationForm.error) {
        this.$set(this.registrationForm, `error.${field}`, false);
      }
    },
    colleageChanged(value) {
      for (let i = 0; i < this.collegeAndMajor.length; i++) {
        if (this.collegeAndMajor[i].collegeId === value) {
          this.majorList = this.collegeAndMajor[i].majorList;
          this.registrationForm.majorId = "";
        }
      }
    },
    //提交报名表
    addForm(registrationForm) {
      this.$refs[registrationForm].validate((valid) => {
        if (valid) {
          addJoinForm(this.registrationForm).then((res) => {
            if (res.code == 20000) {
              this.$message.success("报名成功！");
              this.$router.push("/");
              this.dialogVisible = true;
            } else {
              this.$message.error(res.data.message);
            }
          });
        } else {
          this.$message.error("请检查是否输入正确的数据");
        }
      });
    },
    //根据token查报名表，如果有报名表信息则将信息展示在输入框中，并且关闭输入框
    queryJoinForm() {
      getJoinFormByToken().then((res) => {
        if (res.code == 20000 && res.data.joinForm) {
          this.registrationForm.name = res.data.joinForm.name;
          this.registrationForm.majorId = res.data.joinForm.majorId;
          this.registrationForm.collegeId = res.data.joinForm.collegeId;
          this.registrationForm.phone = res.data.joinForm.phone;
          this.registrationForm.gender = res.data.joinForm.gender;
          this.registrationForm.description = res.data.joinForm.description;
          this.registrationForm.deptId = Number(res.data.joinForm.deptId);
          //控制表单内容是否禁用
          this.isDisabled = true;
          this.isModDisabled = true;
          this.isCancelDisabled = true;
          this.isSignUp = true;
          this.isModBut = true;
        }
      });
    },
    //修改报名表
    mod() {
      this.isModshow = true;
      this.isModBut = false;
      this.isDisabled = false;
    },
    cancelMod() {
      this.isDisabled = true;
      this.isModshow = false;
      this.isModBut = true;
      this.queryJoinForm();
    },
    modJoinForm(registrationForm) {
      this.$refs[registrationForm].validate((valid) => {
        if (valid) {
          modJoinFormByToken(this.registrationForm).then((res) => {
            if (res.code == 20000) {
              this.$message.success(res.message);
              this.isDisabled = true;
              this.isModshow = false;
              this.isModBut = true;
            } else {
              this.$message.error("修改报名表失败，请稍后再试！");
              this.isDisabled = true;
              this.isModshow = false;
              this.isModBut = true;
            }
          });
        } else {
          this.$message.error("请检查是否输入正确的数据");
        }
      });
    },
    save(registrationForm) {
      this.isSignUpBtu = true;
      if (this.isSignUp) {
        this.modJoinForm("registrationForm");
      } else {
        this.addForm("registrationForm");
      }
    },
    //删除报名表
    delJoinForm() {
      delJoinFormByToken().then((res) => {
        if (res.code == 20000) {
          this.$message.success(res.message);
          this.queryJoinForm();
          // this.$router.push("/")
        } else {
          this.$message.error(res.message);
        }
      });
    },

    initForm() {
      collegeQueryAll().then((res) => {
        this.collegeAndMajor = res.data.data.collegeAndMajor;
        for (let i = 0; i < this.collegeAndMajor.length; i++) {
          if (
            this.collegeAndMajor[i].collegeId ===
            this.registrationForm.collegeId
          ) {
            this.majorList = this.collegeAndMajor[i].majorList;
          }
        }
      });
    },
  },

  watch: {
    // 监听表单字段的变化
    // "registrationForm.name"(newVal) {
    //   this.showErrorMessage("name", newVal);
    // },
    // 为其他字段添加类似的监听器
  },
  mounted() {
    // 组件挂载后重置错误信息
    this.initForm();
    this.resetErrors();
    this.queryJoinForm();
  },
};
</script>

<style scoped>
.button-center {
  display: flex;
  justify-content: center;
  align-items: center;
  /* 根据需要决定是否需要垂直居中 */
  margin-left: -45px;
}

/* 如果需要调整按钮之间的间距，可以给按钮添加样式 */
.el-button {
  margin: 0 10px;
  /* 根据需要调整间距 */
}

.error-message {
  color: red;
  /* 提示信息的颜色 */
  font-size: 12px;
  /* 提示信息的字体大小 */
}

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}

.center {
  text-align: center;
}

.grid-content {
  /* border-radius: 4px; */
  min-height: 36px;
}

.flex-warp {
  flex-wrap: wrap;
}

.login-title {
  /* border: 1px solid black; */
  margin-top: 5%;
  margin-bottom: 5%;
}

.login-title h1,
.login-title img {
  display: inline-block;
  vertical-align: middle;
}

.login-title img {
  height: 32px;
  vertical-align: middle;
}

.login-title h1 {
  font-size: 20px;
  font-weight: 600;
  /* vertical-align: middle; */
  display: inline-block;
  line-height: 32px;
}

.login-wrap {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding-top: 5%;

  /* background-color: #112346; */
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
}

.login-container {
  border-radius: 10px;
  margin: 0px auto;
  width: 520px;
  height: 600px;
  padding: 0px 35px 0px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: space-around;
}

.app {
  width: 100%;
  height: 750px;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: linear-gradient(270deg, #f1e1ff, #b8ccff);
}

.box {
  width: 100%;
  height: 100%;
  position: relative;
}

.circle-box {
  width: 100%;
  height: 100%;
  border-radius: 10px;
  position: absolute;
  overflow: hidden;
}

.circle:first-child {
  width: 200px;
  height: 200px;
  border-radius: 50%;
  border: 30px solid #afe7ff;
  box-sizing: border-box;
  position: absolute;
  top: -60px;
  left: -70px;
  animation: move-y 3.5s linear infinite;
}

.circle:last-child {
  width: 150px;
  height: 160px;
  border-radius: 50%;
  background: linear-gradient(136deg, #e6ecff 0%, #a4b4fc 100%);
  box-sizing: border-box;
  position: absolute;
  bottom: -30px;
  right: -30px;
  animation: move-y 5s ease-in-out infinite;
}

.bg-filter {
  width: 100%;
  height: 100%;
  background: rgba(255, 255, 255, 0.05);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(8px);
  border-radius: 10px;
  box-sizing: border-box;
  position: absolute;
}

@keyframes move-y {
  0% {
    transform: translateY(0);
  }

  50% {
    transform: translateY(-16px);
  }

  100% {
    transform: translateY(0);
  }
}

.bubble-bgwall {
  overflow: hidden;
  position: fixed;
  margin: 0 auto;
  width: 100%;
  height: 100%;
  padding: 0;
}

.bubble-bgwall li {
  display: flex;
  position: absolute;
  bottom: -200px;
  justify-content: center;
  align-items: center;
  border-radius: 10px;
  width: 50px;
  height: 50px;
  background-color: rgba(255, 255, 255, 0.15);
  color: #ccc;
  animation: bubble 15s infinite;
  z-index: 999;
}

.bubble-bgwall li:nth-child(1) {
  left: 10%;
}

.bubble-bgwall li:nth-child(2) {
  left: 20%;
  width: 90px;
  height: 90px;
  animation-duration: 7s;
  animation-delay: 2s;
}

.bubble-bgwall li:nth-child(3) {
  left: 25%;
  animation-delay: 4s;
}

.bubble-bgwall li:nth-child(4) {
  left: 40%;
  width: 60px;
  height: 60px;
  background-color: rgba(255, 255, 255, 0.3);
  animation-duration: 8s;
}

.bubble-bgwall li:nth-child(5) {
  left: 70%;
}

.bubble-bgwall li:nth-child(6) {
  left: 80%;
  width: 120px;
  height: 120px;
  background-color: rgba(255, 255, 255, 0.2);
  animation-delay: 3s;
}

.bubble-bgwall li:nth-child(7) {
  left: 32%;
  width: 160px;
  height: 160px;
  animation-delay: 2s;
}

.bubble-bgwall li:nth-child(8) {
  left: 55%;
  width: 40px;
  height: 40px;
  font-size: 12px;
  animation-duration: 15s;
  animation-delay: 4s;
}

.bubble-bgwall li:nth-child(9) {
  left: 25%;
  width: 40px;
  height: 40px;
  background-color: rgba(255, 255, 255, 0.3);
  font-size: 12px;
  animation-duration: 12s;
  animation-delay: 2s;
}

.bubble-bgwall li:nth-child(10) {
  left: 85%;
  width: 40px;
  height: 40px;
  animation-delay: 5s;
}

@keyframes bubble {
  0% {
    opacity: 0.5;
    transform: translateY(0) rotate(45deg);
  }

  25% {
    opacity: 0.75;
    transform: translateY(-400px) rotate(90deg);
  }

  50% {
    opacity: 1;
    transform: translateY(-600px) rotate(135deg);
  }

  100% {
    opacity: 0;
    transform: translateY(-1000px) rotate(180deg);
  }
}
</style>
